
<link rel="stylesheet" type="text/css" href="/javascript/jquery/tree.css">
<script type="text/javascript" src="/javascript/jquery/jquery.simple.tree.js"></script>

<script type="text/javascript">

function num_ord(n){
	var parent_id
	$('*.simpleTree').find('li[id='+n+'] > ul > li[id]').each(function(i) {					
		parent_id =$(this).parent().parent().attr('id');
		if(parent_id==0) 
		  parent_id="";
		$(this).attr('parent_id',parent_id);
		$(this).attr('ord',i);
		 	 
		num_ord($(this).attr('id'));	
	});

	
}

var simpleTreeCollection;
$(document).ready(function(){
	var str = $("#cattree").html();
	$("#cattree").remove();
	$("#crud_list_items").before(str);
	simpleTreeCollection = $('.simpleTree').simpleTree({
		autoclose: true,
		afterClick:function(node){		
			//alert("text-"+$('span:first',node).text());
		},
		afterDblClick:function(node){
			//alert("text-"+$('span:first',node).text());
		},
		afterMove:function(destination, source, pos){
			
			/*$.post('/catalog/move/', {parent_id:+destination.attr('id'), cat_id:source.attr('id'), pos:pos }, function(data) {
		
			}, 'json');*/			
			//alert("destination-"+destination.attr('id')+" source-"+source.attr('id')+" pos-"+pos);
			num_ord(0);
			
		},
		afterAjax:function()
		{
			//alert('Loaded');
		},
		animate:true
		//,docToFolderConvert:true
	});
});



$(function() {
		var arr="";
		
		$("#save_cat").click(function() {	
			$('.simpleTree').find('li[id]').each(function(i) {					
					if($(this).attr('id') > 0)
						arr += $(this).attr('id')+':'+$(this).attr('parent_id')+':'+$(this).attr('ord')+'|';
				});
			$.post('/catalog/save/', {arr:arr}, function(data) {
					alert("Сохранено!");
			}, 'json');
		});
});


</script>

<?
$select = ProductCategoryPeer::getInstance()->select()->order('order');
$PCRows = ProductCategoryPeer::getInstance()->fetchAll($select);
$subPCRows = clone $PCRows;
$subPCRows1 = clone $PCRows;
?>



<div id='cattree'>
<ul class="simpleTree">
	<li class="root" id='0'><span>Категории</span>
		<ul>
			<?
			foreach($PCRows as $pcRow){
				$str="";
				if($pcRow->getParentId()==0){
					$str= "<li name='qq' id='".$pcRow->getId()."' parent_id='".$pcRow->getParentId()."' ord='".$pcRow->getOrder()."'><span>".$pcRow->getName()."</span>";
					$substr="";
					foreach($subPCRows as $subpcRow){					  
						if($subpcRow->getParentId()==$pcRow->getId()){
							$substr.= "<li name='qq' id='".$subpcRow->getId()."' parent_id='".$subpcRow->getParentId()."' ord='".$pcRow->getOrder()."'><span>".$subpcRow->getName()."</span>";
							$substr1="";								
							foreach($subPCRows1 as $subpcRow1){					  
								if($subpcRow1->getParentId()==$subpcRow->getId()){
									$substr1.= "<li name='qq' id='".$subpcRow1->getId()."' parent_id='".$subpcRow1->getParentId()."' ord='".$pcRow->getOrder()."'><span>".$subpcRow1->getName()."</span></li>";									
								}
							}									
							if(strlen($substr1)>0)
									$substr1 = "<ul>".$substr1."</ul>";
							$substr.=$substr1."</li>";
						}
					}
					if(strlen($substr)>0)
						$substr = "<ul>".$substr."</ul>";
					$str.=$substr."</li>";					
					echo $str;
				}
			}
			?>		
		</ul>
	</li>
</ul>
<br>
<input type="button" id="save_cat" value="Сохранить порядок">
<br>
</div>